@use 'sass:map';

@use './shared' as *;
@use './design' as *;

$switch: () !default;
$switch: map.merge(
  (
    bg-color: get-css-var('fill-color-secondary'),
    bg-color-open: get-css-var('color-primary-base'),
    bg-color-disabled: get-css-var('fill-color-disabled'),
    opacity-disabled: 60%,
    opacity-loading: get-css-var('switch-opacity-disabled'),
    s-color-focus: get-css-var('color-primary-opacity-6'),
    label-color: get-css-var('color-white'),
    height: 22px,
    signal-size: calc(get-css-var('switch-height') - 4px),
    signal-bg-color: get-css-var('color-white'),
    series-span: 8px,
    icon-color: get-css-var('color-black'),
    icon-color-loading: get-css-var('color-primary-base'),
    circle-opacity: 0,
    radius-rectangle: get-css-var('radius-base')
  ),
  $switch
);

.#{$namespace}-switch {
  &-vars {
    @include define-preset-values('switch', $switch);
  }

  @mixin define-switch-style($style-map) {
    @include define-preset-style('switch', $style-map);
  }

  $height: get-css-var('switch-height');
  $signal: get-css-var('switch-signal-size');

  @include basis;

  position: relative;
  display: inline-flex;
  align-items: center;
  vertical-align: middle;
  cursor: pointer;
  user-select: none;
  background-color: get-css-var('switch-bg-color');
  border-radius: $height;
  transition: get-css-var('transition-background'), get-css-var('transition-border'),
    get-css-var('transition-shadow'), get-css-var('transition-opacity');

  &--small {
    @include define-preset-values(
      'switch',
      (
        height: 18px
      )
    );
  }

  &--large {
    @include define-preset-values(
      'switch',
      (
        height: 26px
      )
    );
  }

  & + & {
    margin-inline-start: get-css-var('switch-series-span');
  }

  &:focus-within {
    box-shadow: get-css-var('shadow-focus') get-css-var('switch-s-color-focus');
  }

  &--loading {
    cursor: default;
    opacity: get-css-var('switch-opacity-loading');
  }

  &--open {
    background-color: get-css-var('switch-bg-color-open');
  }

  &__placeholder {
    display: flex;
    flex-direction: column;
    min-width: calc(#{$height} * 2);
    height: $height;
    padding-inline-start: calc(#{$height} * 1.5 + 1px);
    overflow: hidden;
    white-space: nowrap;
    pointer-events: none;
    visibility: hidden;
  }

  &__label {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding-inline: calc(#{$height} + 2px) calc(#{$height} * 0.5 - 1px);
    line-height: 1;
    color: get-css-var('switch-label-color');
    border-radius: $height;
    transition: padding get-css-var('transition-base');
  }

  &--open &__label {
    padding-inline: calc(#{$height} * 0.5 - 1px) calc(#{$height} + 2px);
  }

  &__open-text,
  &__close-text {
    display: flex;
    white-space: nowrap;
  }

  &__signal {
    position: absolute;
    inset-inline-start: calc((#{$height} - #{$signal}) * 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    width: $signal;
    height: $signal;
    background-color: get-css-var('switch-signal-bg-color');
    border-radius: 50%;
    transition:
      inset-inline-start get-css-var('transition-base'),
      get-css-var('transition-transform');
    will-change: inset-inline-start;

    .#{$namespace}-icon {
      color: get-css-var('switch-icon-color');
    }
  }

  &--open &__signal {
    inset-inline-start: calc(100% - #{$signal} - (#{$height} - #{$signal}) * 0.5);
  }

  &--loading &__signal .#{$namespace}-icon {
    color: get-css-var('switch-icon-color-loading');
  }

  &--rectangle,
  &--rectangle &__label,
  &--rectangle &__signal {
    border-radius: get-css-var('switch-radius-rectangle');
  }

  $states: success, error, warning;

  @each $state in $states {
    &--#{$state} {
      @include define-switch-style(
        (
          bg-color: 'color' $state 'light-1',
          bg-color-open: 'color' $state 'light-1',
          s-color-focus: 'color' $state 'opacity-6',
          circle-opacity: 80%
        )
      );
    }
  }

  &--disabled {
    cursor: not-allowed;
    background-color: get-css-var('switch-bg-color-disabled');
    opacity: get-css-var('switch-opacity-disabled');
  }

  &--disabled &__signal {
    pointer-events: none;
  }

  &__input {
    width: 0;
    height: 0;
    margin: 0;
    pointer-events: none;
    opacity: 0%;
  }
}
